<?php

namespace yan\backendclient\modules\backendmodule\controllers;

use Yii;
use yan\backendclient\modules\backendmodule\models\BackendModule;
use yii\data\ArrayDataProvider;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * DefaultController implements the CRUD actions for BackendModule model.
 */
class DefaultController extends \yan\backendclient\controllers\DebugController
{
    

    /**
     * Lists all BackendModule models.
     * @return mixed
     */
    public function actionIndex()
    {
        $model  = new BackendModule;


        $dataProvider = new ArrayDataProvider([
            'allModels' => $model->listModules(),
        ]);

        return $this->render('index', [
            'dataProvider' => $dataProvider,
        ]);
    }

    public function actionUpdate($id){
        $model  = new BackendModule;
        if(Yii::$app->request->isPost && 
        !empty(Yii::$app->request->post('table')) &&
        !empty(Yii::$app->request->post('do'))
        ){
            try {
                if($model->tableDo(Yii::$app->request->post('do'),$id,Yii::$app->request->post('table'))){
                    Yii::$app->getSession()->setFlash('message', Yii::t('debug', 'Successful'));
                    return $this->redirect(!empty(\Yii::$app->request->referrer) ? \Yii::$app->request->referrer : ['index']);
                }
            } catch (\Throwable $th) {
                Yii::$app->getSession()->setFlash('message', $th->getMessage());
            }
            
        }
        return $this->render('update', ['list'=>$model->getSqlList($id)]);
    }
    // 啟動和關閉
    public function actionView($id){
        $model  = new BackendModule;

        switch (Yii::$app->request->get('type')) {
            case 'install':
                $model->installDo($id);
                break;
            case 'uninstall':
                $model->uninstallDo($id);
                break;
            case 'remove':
                $model->removeDo($id);
                break;
            default:
                $model->activeDo($id);
                break;
        }
        Yii::$app->getSession()->setFlash('message', Yii::t('debug', 'Successful'));
        return $this->redirect(!empty(\Yii::$app->request->referrer) ? \Yii::$app->request->referrer : ['index']);
    }
    
}
